using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AandEDLL;
using AandEDLL.DataAccess;

namespace ASA
{
    public partial class EditUserDialog : ASA.BaseDialog
    {
        private const String EDIT = "edit";
        private const String NEW = "new";
        private User user;
        private String mode;

        public EditUserDialog()
        {
            InitializeComponent();
            this.Title = "Add New User";
            this.Text = "Add New User";
            mode = NEW;
        }
        public EditUserDialog(User user)
        {
            InitializeComponent();
            this.user = user;
            tbUserName.Text = this.user.UserName;
            tbUserName.Enabled = false;
            tbPassword.Text = user.Password;
            if (user.UserName.Equals("Admin"))
            {
                cbAdmin.Checked = true;
                cbAdmin.Enabled = false;
            }
            cbAdmin.Checked = user.Admin;
            mode = EDIT;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (mode.Equals(EDIT))
            {
                user.Password = tbPassword.Text;
                user.Admin = cbAdmin.Checked;
                

                try
                {
                    if (tbPassword.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("Password field cannot be empty.");
                        return;
                    }
                    if (Accessor.UpdateUser(user))
                    {
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("Unable to update user \"" + user.UserName + "\"", "Warning");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error Updating User");
                }
            }
            else if (mode.Equals(NEW))
            {
                if (tbUserName.Text.Equals("") || tbPassword.Text.Equals(""))
                {
                    MessageBox.Show("A user name and password must be specified", "Add User Exception");
                    return;
                }
                user = new User();
                user.UserName = tbUserName.Text;
                user.Password = tbPassword.Text;
                user.Admin = cbAdmin.Checked;

                // validating if the user already exitsw

                List<AandEDLL.DataAccess.User> list = AandEDLL.Accessor.SelectUser();
                foreach (AandEDLL.DataAccess.User dbUser in list)
                {
                    if (dbUser.UserName == user.UserName)
                    {
                        MessageBox.Show(this, "User \"" + user.UserName + "\" already exists, enter a different username.", "User Name Already exists", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        return;
                    }
                }

                try
                {
                    if (Accessor.CreateUser(user) > -1)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An exception occured with creating User. The error is: " + ex.Message,
                        "Add User Exception");
                }
            }
        }

        
        private void newUser()
        {
        }
    }
}

